/*
 * FramConvertGps.java
 *
 * Created on 1 septembre 2001, 15:49
 */

package sma.gps.convert;

import java.awt.FileDialog;
import java.io.File;

import sma.gps.fich.FichGps;
import sma.gps.fich.FichMpsTxt;
import sma.gps.fich.FichOzi;
import sma.gps.fich.FichPcx5;

/**
 *
 * @author  MARSOLLE
 * @version
 */
public class FramConvertGps extends java.awt.Frame {
  private String direct = "D:\\GPS\\R&R\\modif\\";
  
    /** Creates new form FramConvertGps */
  public FramConvertGps() {
    initComponents ();
    pack ();
  }
  
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
  private void initComponents() {//GEN-BEGIN:initComponents
    tfRep = new java.awt.TextField();
    butRep = new java.awt.Button();
    taLog = new java.awt.TextArea();
    butPcxOzi = new java.awt.Button();
    
    setLayout(new java.awt.GridBagLayout());
    java.awt.GridBagConstraints gridBagConstraints1;
    
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent evt) {
        exitForm(evt);
      }
    });
    
    tfRep.setText("chemin");
    tfRep.setBackground(java.awt.Color.white);
    tfRep.setFont(new java.awt.Font("Dialog", 0, 11));
    tfRep.setColumns(50);
    tfRep.setForeground(java.awt.Color.black);
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 0;
    gridBagConstraints1.gridy = 0;
    gridBagConstraints1.gridwidth = 2;
    gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
    add(tfRep, gridBagConstraints1);
    
    butRep.setFont(new java.awt.Font("Dialog", 0, 11));
    butRep.setLabel("R\u00e9pertoire...");
    butRep.setName("butRep");
    butRep.setBackground(new java.awt.Color(224, 204, 152));
    butRep.setForeground(java.awt.Color.black);
    butRep.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        butRepActionPerformed(evt);
      }
    });
    
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 2;
    gridBagConstraints1.gridy = 0;
    add(butRep, gridBagConstraints1);
    
    taLog.setText("S\u00e9lectionner un r\u00e9pertoire de travail\npuis");
    taLog.setBackground(new java.awt.Color(224, 204, 152));
    taLog.setEditable(false);
    taLog.setFont(new java.awt.Font("Dialog", 0, 11));
    taLog.setForeground(java.awt.Color.black);
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 0;
    gridBagConstraints1.gridy = 2;
    gridBagConstraints1.gridwidth = 3;
    gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
    gridBagConstraints1.weighty = 5.0;
    add(taLog, gridBagConstraints1);
    
    butPcxOzi.setFont(new java.awt.Font("Dialog", 0, 11));
    butPcxOzi.setLabel("PCX5 -> Ozi");
    butPcxOzi.setName("button5");
    butPcxOzi.setBackground(new java.awt.Color(224, 204, 152));
    butPcxOzi.setForeground(java.awt.Color.black);
    butPcxOzi.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        butPcxOziActionPerformed(evt);
      }
    });
    
    gridBagConstraints1 = new java.awt.GridBagConstraints();
    gridBagConstraints1.gridx = 0;
    gridBagConstraints1.gridy = 1;
    add(butPcxOzi, gridBagConstraints1);
    
  }//GEN-END:initComponents
  
  private void butPcxOziActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butPcxOziActionPerformed
    taLog.setText("");
    
    File repertoire = new File(direct);
    String[] listFich = repertoire.list();
    
    for (int i=0 ; i < listFich.length ; i++) {
      FichGps source = new FichPcx5();
      source.setNomFichier( direct + listFich[i] );
      if (! source.isCompatible() ) {
        source = new FichMpsTxt();
        source.setNomFichier( direct + listFich[i] );
        if (! source.isCompatible() ) {
          source = null;
        }
      }
      
      if ( source != null ) {
        logLn( listFich[i] );
        try {
          source.loadFile();
          FichOzi cible = new FichOzi();
          cible.configGps = source.configGps;
          // l'extension sera ajout�e automatiquement
          cible.baseName = source.getNomFichier();
          System.out.println("avant saveFile " + cible.baseName);
          cible.saveFile();
          
        } catch (Exception e) {
          logLn( "Pb... ) " + e );
          e.printStackTrace();
          break;
        }
      }
    }
    
  }//GEN-LAST:event_butPcxOziActionPerformed
  
  private void butRepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butRepActionPerformed
    tfRep.setText("?");
    taLog.setText("");
    FileDialog fd = changeRep();
    
    File repertoire = new File(direct);
    String[] listFich = repertoire.list();
    
    FichGps fich;
    
    for (int i=0 ; i < listFich.length ; i++) {
      //listFich[i].
			fich = getFichGps(direct + listFich[i]);
      if ( fich != null ) {
        logLn( listFich[i] + "  " + fich );
      }
    }
    
  }//GEN-LAST:event_butRepActionPerformed
  
	/**
	 * renvoie un gestionnaire de fichier approprié
	 */
	private FichGps getFichGps(String nomF) {
		FichGps fich;

		fich = new FichPcx5();
		fich.setNomFichier(nomF);
		if (fich.isCompatible())
			return fich;

		fich = new FichOzi();
		fich.setNomFichier(nomF);
		if (fich.isCompatible())
			return fich;

		fich = new FichMpsTxt();
		fich.setNomFichier(nomF);
		if (fich.isCompatible())
			return fich;
		return null;
	}

	/** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
      System.exit (0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main (String args[]) {
      new FramConvertGps ().show ();
    }
    
    private FileDialog changeRep() {
      FileDialog fd = new  FileDialog(this);
      fd.setDirectory( direct );
      fd.setFile("*.trk;*.wpt;*.grm;*.rte;*.txt");
      fd.show();
      direct = fd.getDirectory();
      tfRep.setText( direct );
      return fd;
    }
    // pour logguer les message dans la text area
    public void logLn(String message) {
      taLog.append(message + "\n");
      System.out.println(message);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextField tfRep;
    private java.awt.Button butRep;
    private java.awt.TextArea taLog;
    private java.awt.Button butPcxOzi;
    // End of variables declaration//GEN-END:variables
    
}
